package com.apptravel.web.controller.admin;

import com.apptravel.entity.Area;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2012-1-12
 * Time: 12:33:14
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/admin/area")
public class AdminAreaController extends AdminAreaBaseController{

    private static final String NODES = "/nodes";

    private static final String UPDATE = "/update";

    private static final String ADD  ="/add";

    private static final String DEL = "/del";

    private static final String HOME_VIEW = "admin/area/home";

    /**
     * 列表
     *
     * @return
     */
    @RequestMapping()
    public String home(){

        return HOME_VIEW;
    }

    /**
     * ȡ子节点.
     *
     * @return
     */
    @RequestMapping(NODES)
    public String nodes(ModelMap model,Long id){
        Long parentId = 0L;
        if(id != null && id > 0){
            parentId = id;
        }
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("parentid",parentId);
        List<Area> areas = areaDao.getListNoPage(params);
        List<AreaNode> nodes = new ArrayList<AreaNode>();
        for(Area area:areas){
            AreaNode node = new AreaNode();
            node.setId(area.getId());
            node.setName(area.getAreaname());
            node.setIsParent(this.haveChilds(area));
            nodes.add(node);
        }
        model.put("nodes",nodes);
        return null;
    }

    /**
     * 更新
     *
     * @param model
     * @param id
     * @param name
     * @return
     */
    @RequestMapping(UPDATE)
    public String update(ModelMap model,Long id,String name){
        Area area = areaDao.selectById(id);
        if(area != null){
            area.setAreaname(name);
            areaDao.update(area);
        }
        model.put("result","0");
        
        return null;
    }

    /**
     * 删除
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(DEL)
    public String del(ModelMap model,Long id){
        areaDao.del(id);
        model.put("result","0");
        return null;
    }

    /**
     * 添加.
     * 
     * @param model
     * @param name
     * @param pid
     * @return
     */
    @RequestMapping(ADD)
    public String add(ModelMap model,String name,Long pid){
        Area area = new Area();
        area.setAreaname(name);
        area.setParentid(pid);
        areaDao.insert(area);
        model.put("result","0");
        return null;
    }



}
